package com.hdey.controller.chao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdey.mapper.chao.KeShiMapper;
import com.hdey.pojo.chao.Keshi;
import com.hdey.service.chao.KeshiService;
import com.hdey.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;

@RestController
@RequestMapping("/List")
@Slf4j
public class RecordController {

        @Resource
        private KeshiService keshiService;

        @Resource
        private KeShiMapper keShiMapper;

        private String msg;

        @GetMapping("/index")
        public ModelAndView index(){

            return new ModelAndView("List");

        }
//        // 添加
//        @RequestMapping("/add")
//        public Result add(Wuzi wuzi) {
//                System.out.println(wuzi);
//                wuzi.setCreateDate(LocalDate.now());
//                boolean flag = wuziService.save(wuzi);
//                return flag ? Result.success("添加成功") : Result.fail("添加失败");
//        }

        @RequestMapping("/search")
        @ResponseBody
        public Result search(Integer page, Integer limit, String departmentName){
                Page<Keshi> keshiPage = keshiService.searchPage(page, limit, departmentName);
                System.out.println(keshiPage.getRecords());
                return Result.success(keshiPage);
        }

        // 添加
        @RequestMapping("/add")
        public Result add(Keshi keshi) {
                keshi.setCreateDate(LocalDate.now());
                boolean flag = keshiService.save(keshi);
                return flag ? Result.success("添加成功") : Result.failed("添加失败");
        }



        // 删除
        @RequestMapping("/del")
        public Result del(Integer departmentId){
                //System.out.println(departmentId);

                LambdaQueryWrapper<Keshi> wrapper=new LambdaQueryWrapper<>();
                wrapper.eq(Keshi::getDepartmentId,departmentId);
                int  flag = keShiMapper.delete(wrapper);
//                String msg = "";
//
//                if(flag){
//                        msg = "删除成功!";
//                }else {
//                        msg = "删除失败";
//                }
//                return Result.fail(msg);
                return flag > 0 ? Result.success("删除成功") : Result.failed("删除失败");
        }



}
